package com.qf.service.Impl;

import com.qf.annotations.RequestMapping;
import com.qf.dao.OrderDao;
import com.qf.dao.gouwuche;
import com.qf.dao.impl.OrderDaoImpl;
import com.qf.pojo.*;
import com.qf.service.OderService;

import java.util.List;
import java.util.UUID;

public class OderServiceImpl implements OderService {
    OrderDao orderDao = new OrderDaoImpl();
    @Override
    public Order createOrder(List<CartItem> cartItemList, String aid) {
        if (cartItemList != null && cartItemList.size()>0){
            String oid = UUID.randomUUID().toString().replace("-","");
            long total = cartItemList.stream().mapToLong(CartItem::getTotalPrice).sum();
            Long uid = cartItemList.get(0).getUid();
            orderDao.addOrder(oid, uid, total, aid);
            cartItemList.forEach(cartItem -> {
                long id = cartItem.getId();
                long totalPrice = cartItem.getTotalPrice();
                int num = cartItem.getNum();
                orderDao.addOrderDetail(oid, id, num, totalPrice);
            });
            Order order = new Order();
            order.setMoney(total);
            order.setId(oid);
            return order;
        }
        return null;
    }

    @Override
    public void processPay(String id) {

        String status = orderDao.findStatusById(id);
        if ("0".equalsIgnoreCase(status)){
            orderDao.updateStaus2Success(id);
        }else {
            System.err.println("当前订单状态异常，无法更新为支付 状态");
        }
    }
    @Override
    public List<TbOrder> showOrder(long uid) {
        List<TbOrder> showorder = orderDao.showorder(uid);
        return showorder;
    }

    @Override
    public TbOrder showkz(String id) {
        TbOrder showkz = orderDao.showkz(id);
        return showkz;
    }

    @Override
    public List<TbOrderdetail> showde(String id) {
        List<TbOrderdetail> showkz = orderDao.showde(id);
        return showkz;
    }

    @Override
    public void deletegouwu(String id) {
        orderDao.deletegouwu(id);
    }

}
